from itsdangerous import TimedJSONWebSignatureSerializer
from django.conf import settings
from itsdangerous import BadData

def generate_access_token(openid):
    # 创建对象
    obj = TimedJSONWebSignatureSerializer(settings.SECRET_KEY, expires_in=600)

    data = {'openid': openid}
    # 对字典进行加密
    token = obj.dumps(data)

    # 解码返回
    return token.decode()

def check_access_token(access_token):
    obj = TimedJSONWebSignatureSerializer(settings.SECRET_KEY, expires_in=600)
    try:
        data = obj.loads(access_token)
    except BadData:
        return None
    else:
        return data.get('openid')
